﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kaskelot.Services.Model;

namespace Stockholm.SOA.Kaskelot.WCF.ServiceImplementation.Mapping
{
	public static class PersonExtensions
	{
		public static DataContracts.Person ToContract(this Person entity)
		{
			DataContracts.Person b = new DataContracts.Person();

			CopyFromEntity(entity, b, false);

			return b;
		}

		public static DataContracts.Person ToMinimalContract(this Person entity)
		{
			DataContracts.Person b = new DataContracts.Person();

			CopyFromEntity(entity, b, true);

			return b;
		}

		public static Person ToEntity(this DataContracts.Person b)
		{
			Person entity = new Person();

			CopyToEntity(b, entity);

			return entity;
		}

		public static void UpdateEntity(this Person entitiy, DataContracts.Person person)
		{
			CopyToEntity(person, entitiy);
		}

		private static void CopyFromEntity(Person from, DataContracts.Person to, bool minimal)
		{
			to.Id = from.Id;
			to.ValId = from.ValId;

			to.Fornamn = from.Fornamn;
			to.Efternamn = from.Efternamn;
			to.Personnummer = from.Personnummer;
			to.Epost = from.Epost;
			to.TelNr = from.TelNr;
			to.MobTelNr = from.MobTelNr;
			to.GatuAdress = from.Gatuadress;
			to.PostNr = from.PostNr;
			to.PostOrt = from.PostOrt;
			to.Fritext = from.Fritext;
			to.Typer = from.PersonPersonTyp.Select(ppt => ppt.PersonTyp.ToContract()).ToArray();
			to.Inaktiv = from.Inaktiv;
			to.AnmalanGodkand = from.AnmalanGodkand;
			to.AnsokanGodkand = from.AnsokanGodkand;

			to.Andrad = from.Andrad;
			to.AndradAv = from.AndradAv;
			to.Skapad = from.Skapad;
			to.SkapadAv = from.SkapadAv;
		}

		private static void CopyToEntity(DataContracts.Person from, Person to)
		{
			to.Id = from.Id;
			to.ValId = from.ValId;

			to.Fornamn = from.Fornamn;
			to.Efternamn = from.Efternamn;
			to.Personnummer = from.Personnummer;
			to.Epost = from.Epost;
			to.TelNr = from.TelNr;
			to.MobTelNr = from.MobTelNr;
			to.Gatuadress = from.GatuAdress;
			to.PostNr = from.PostNr;
			to.PostOrt = from.PostOrt;
			to.Fritext = from.Fritext;

			to.Inaktiv = from.Inaktiv;
			to.AnmalanGodkand = from.AnmalanGodkand;
			to.AnsokanGodkand = from.AnsokanGodkand;

			to.Andrad = from.Andrad;
			to.AndradAv = from.AndradAv;
			to.Skapad = from.Skapad;
			to.SkapadAv = from.SkapadAv;
		}

		public static void UpdatePersonPersonTyper(this DataContracts.Person from, Person to, KaskelotEntities ctx)
		{
			List<PersonPersonTyp> objectsToRemove = to.PersonPersonTyp.Where(ppt => !from.Typer.Any(t => t.Id == ppt.PersonTypId)).ToList();
			foreach (PersonPersonTyp ppt in objectsToRemove)
				ctx.PersonPersonTyp.Remove(ppt);

			List<DataContracts.PersonTyp> objectsToAdd = from.Typer.Where(t => !to.PersonPersonTyp.Any(ppt => ppt.PersonTypId == t.Id)).ToList();
			foreach (DataContracts.PersonTyp pt in objectsToAdd)
				ctx.PersonPersonTyp.Add(new PersonPersonTyp()
				{
					Id = Guid.NewGuid(),
					ValId = pt.ValId,
					PersonId = from.Id,
					PersonTypId = pt.Id
				});
		}
	}
}
